package com.gitee.dolt.boot.controller;

import com.gitee.dolt.annotation.log.SysLog;
import com.gitee.dolt.annotation.security.PreAuth;
import com.gitee.dolt.boot.request.QueryParams;
import com.gitee.dolt.boot.service.SuperCacheService;
import com.gitee.dolt.core.base.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;

import java.io.Serializable;


/**
 * 带缓存的控制器
 *
 * @author LYH
 */
@Slf4j
public abstract class SuperCacheController<Entity, Service extends SuperCacheService<Entity>, ID extends Serializable, SaveDTO, UpdateDTO, QueryDTO>
        extends SuperController<Entity, Service, ID, SaveDTO, UpdateDTO, QueryDTO> {
    @Override
    public Result handlerGet(ID id) {
        return Result.ok(baseService.getByIdCache(id));
    }

    @Override
    public Result handlerQuery(QueryParams<QueryDTO> params) {
        log.info("query由于使用了缓存，查询条件[QueryParams]失效");
        return Result.ok(baseService.listByCache()).setMessage("此处使用了缓存，返回所有数据，查询条件无效。");
    }

    @ApiOperation(value = "清除缓存")
    @PostMapping("/clear-cache")
    @SysLog("清除缓存")
    @PreAuth("hasAnyPermission('inner:clear-cache')")
    public Result clearCache() {
        baseService.clearCache();
        return Result.ok();
    }

    @ApiOperation(value = "刷新缓存[慎用，会查询整个表。需要预先缓存的可用]")
    @PostMapping("/refresh-cache")
    @SysLog("刷新缓存")
    @PreAuth("hasAnyPermission('inner:refresh-cache')")
    public Result refreshCache() {
        baseService.refreshCache();
        return Result.ok();
    }
}
